home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GXFILL < prev    next >
Text File  |  1991-10-26  |  23KB  |  721 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gxfill.c */
  21. /* Lower-level path filling procedures for GhostScript library */
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gxdevice.h"            /* for gx_color_index */
  27. #include "gzcolor.h"
  28. #include "gzpath.h"
  29. #include "gzstate.h"
  30.  
  31. /* Import the fixed * fixed / fixed routine from gxdraw.c. */
  32. /* The second argument must be less than or equal to the third; */
  33. /* all must be non-negative, and the last must be non-zero. */
  34. extern fixed fixed_mult_quo(P3(fixed, fixed, fixed));
  35.  
  36. /* Define the structure for keeping track of active lines. */
  37. typedef struct active_line_s active_line;
  38. struct active_line_s {
  39.     gs_fixed_point start;        /* x,y where line starts */
  40.     gs_fixed_point end;        /* x,y where line ends */
  41. #define al_dx(alp) ((alp)->end.x - (alp)->start.x)
  42. #define al_dy(alp) ((alp)->end.y - (alp)->start.y)
  43.     fixed y_fast_max;        /* can do x_at_y in fixed point */
  44.                     /* if y <= y_fast_max */
  45. #define set_al_points(alp, startp, endp)\
  46.   (alp)->y_fast_max = max_fixed / (((endp).x > (startp).x ?\
  47.     (endp).x - (startp).x : (startp).x - (endp).x) | 1) + (startp).y,\
  48.   (alp)->start = startp, (alp)->end = endp
  49. #define al_x_at_y(alp, yv)\
  50.   ((yv) == (alp)->end.y ? (alp)->end.x :\
  51.    ((yv) <= (alp)->y_fast_max ?\
  52.     ((yv) - (alp)->start.y) * al_dx(alp) / al_dy(alp) :\
  53.     (stat(n_slow_x),\
  54.      fixed_mult_quo(al_dx(alp), (yv) - (alp)->start.y, al_dy(alp)))) +\
  55.    (alp)->start.x)
  56.     fixed x_current;        /* current x position */
  57.     fixed x_next;            /* x position at end of band */
  58.     segment *pseg;            /* endpoint of this line */
  59.     int direction;            /* direction of line segment */
  60. #define dir_up 1
  61. #define dir_down (-1)
  62.     short tag;            /* distinguish path from clip path */
  63. /* "Pending" lines (not reached in the Y ordering yet) use next and prev */
  64. /* to order lines by increasing starting Y.  "Active" lines (being scanned) */
  65. /* use next and prev to order lines by increasing current X, or if the */
  66. /* current Xs are equal, by increasing final X. */
  67.     active_line *prev, *next;
  68. /* Link together active_lines allocated individually */
  69.     active_line *alloc_next;
  70. };
  71.  
  72. /* Define the ordering criterion for active lines. */
  73. /* The xc argument is a copy of lp2->x_current. */
  74. #define x_precedes(lp1, lp2, xc)\
  75.   (lp1->x_current < xc || lp1->x_current == xc &&\
  76.    (lp1->start.x > lp2->start.x || lp1->end.x < lp2->end.x))
  77.  
  78. #ifdef DEBUG
  79. /* Internal procedures for printing active lines */
  80. private void
  81. print_active_line(char *label, active_line *alp)
  82. {    dprintf6("[f]%s %lx(%d,%d): x_current=%f x_next=%f\n",
  83.              label, (ulong)alp, alp->tag, alp->direction,
  84.              fixed2float(alp->x_current), fixed2float(alp->x_next));
  85.     dprintf5("    start=(%f,%f) pt_end=%lx(%f,%f)\n",
  86.              fixed2float(alp->start.x), fixed2float(alp->start.y),
  87.              (ulong)alp->pseg,
  88.              fixed2float(alp->end.x), fixed2float(alp->end.y));
  89.     dprintf2("    prev=%lx next=%lx\n",
  90.          (ulong)alp->prev, (ulong)alp->next);
  91. }
  92. private void
  93. print_line_list(active_line *flp)
  94. {    active_line *lp;
  95.     for ( lp = flp; lp != 0; lp = lp->next )
  96.        {    fixed xc = lp->x_current, xn = lp->x_next;
  97.         dprintf4("[f]%lx(%d,%d): x_current/next=%g",
  98.                  (ulong)lp, lp->tag, lp->direction,
  99.                  fixed2float(xc));
  100.         if ( xn != xc ) dprintf1("/%g", fixed2float(xn));
  101.         dputc('\n');
  102.        }
  103. }
  104. #define print_al(label,alp)\
  105.   if ( gs_debug['F'] ) print_active_line(label, alp)
  106. #else
  107. #define print_al(label,alp) 0
  108. #endif
  109.  
  110. /* Line list structure */
  111. struct line_list_s {
  112.     active_line *active_area;    /* allocated active_line list */
  113.     line_close_segment *close_area;    /* allocated closing line area */
  114.     uint close_count;
  115.     gs_fixed_rect box;        /* intersection of bounding boxes, */
  116.                     /* disregard lines outside this */
  117.     short tag;            /* tag for lines being added */
  118.     active_line *next_active;    /* next allocation slot */
  119.     active_line *limit;        /* limit of local allocation */
  120.     line_close_segment *next_line;    /* next line allocation slot */
  121.     active_line *y_list;        /* Y-sorted list of pending lines */
  122.     active_line *y_line;        /* most recently inserted line */
  123.     active_line x_head;        /* X-sorted list of active lines */
  124. #define x_list x_head.next
  125.     int no_clip;            /* true if clipping not needed */
  126.         /* Put the arrays last so the scalars will have */
  127.         /* small displacements. */
  128.         /* Allocate a few active_lines and line_close_segments */
  129.         /* locally to avoid round trips through the allocator. */
  130. #define max_local_active 20
  131.     active_line local_active[max_local_active];
  132. #define max_local_close 5
  133.     line_close_segment local_close[max_local_close];
  134. };
  135. typedef struct line_list_s line_list;
  136. typedef line_list _ss *ll_ptr;
  137.  
  138. /* Forward declarations */
  139. private int alloc_line_list(P2(ll_ptr, uint));
  140. private void free_line_list(P1(ll_ptr));
  141. private int add_y_list(P3(gx_path *, short, ll_ptr));
  142. private int add_y_line(P4(segment *, segment *, int, ll_ptr));
  143. private void fill_loop(P5(gx_device_color *, int, ll_ptr,
  144.   gs_state *, fixed));
  145. private void insert_x_new(P2(active_line *, ll_ptr));
  146. private void update_x_list(P2(active_line *, fixed));
  147.  
  148. /* Statistics */
  149. #ifdef DEBUG
  150. #define stat(x) (x++)
  151. #define statn(x,n) (x += (n))
  152. private long n_fill;
  153. private long n_fill_alloc;
  154. private long n_y_up;
  155. private long n_y_down;
  156. private long n_x_step;
  157. private long n_slow_x;
  158. private long n_iter;
  159. private long n_find_y;
  160. private long n_band;
  161. private long n_band_step;
  162. private long n_band_fill;
  163. #else
  164. #define stat(x) 0
  165. #define statn(x,n) 0
  166. #endif
  167.  
  168. /* General area filling algorithm. */
  169. /* The adjust parameter is a hack for keeping small characters */
  170. /* from coming out too faint: it specifies an amount by which to expand */
  171. /* all sides of every filled region. */
  172. int
  173. gx_fill_path(gx_path *ppath, gx_device_color *pdevc, gs_state *pgs,
  174.   int rule, fixed adjust)
  175. {    gx_path *cpath = pgs->clip_path;
  176.     gx_path *pfpath;
  177.     gx_path ffpath;
  178.     int code;
  179.     line_list lst;
  180.     uint sub_count;
  181.     /* Start by flattening the path.  We should do this on-the-fly.... */
  182.     if ( !ppath->curve_count )    /* don't need to flatten */
  183.         pfpath = ppath;
  184.     else
  185.        {    if ( (code = gx_path_flatten(ppath, &ffpath, pgs->flatness)) < 0 ) return code;
  186.         pfpath = &ffpath;
  187.        }
  188.     /* Check the bounding boxes. */
  189. #define ibox lst.box
  190.     gx_path_bbox(pfpath, &ibox);
  191.     if ( ibox.q.y <= cpath->cbox.q.y && ibox.q.x <= cpath->cbox.q.x &&
  192.          ibox.p.y >= cpath->cbox.p.y && ibox.p.x >= cpath->cbox.p.x
  193.        )
  194.        {    /* Path lies entirely within clipping rectangle */
  195.         lst.no_clip = 1;
  196.         sub_count = 0;
  197.        }
  198.     else
  199.        {    /* Intersect the path box and the clip bounding box */
  200. #define int_box(pqxy, rel)\
  201.   if ( cpath->bbox.pqxy rel ibox.pqxy )\
  202.     ibox.pqxy = cpath->bbox.pqxy
  203.         int_box(q.x, <); int_box(q.y, <);
  204.         int_box(p.x, >); int_box(p.y, >);
  205. #undef int_box
  206.         if ( ibox.p.x >= ibox.q.x || ibox.p.y >= ibox.q.y )
  207.            {    /* Intersection of boxes is empty! */
  208.             code = 0;
  209.             goto skip;
  210.            }
  211.         lst.no_clip = 0;
  212.         sub_count = cpath->subpath_count;
  213.        }
  214. #undef ibox
  215.     sub_count += pfpath->subpath_count;
  216.     if ( !(code = alloc_line_list(&lst, sub_count)) )
  217.        {    if ( !lst.no_clip )
  218.            {    if ( (code = add_y_list(cpath, (short)1, &lst)) < 0 )
  219.                 goto nope;
  220.            }
  221.         if ( (code = add_y_list(pfpath, (short)0, &lst)) < 0 )
  222.             goto nope;
  223.         fill_loop(pdevc, rule, &lst, pgs, adjust);
  224. nope:        free_line_list(&lst);
  225.        }
  226. skip:    if ( pfpath != ppath )    /* had to flatten */
  227.         gx_path_release(pfpath);
  228. #ifdef DEBUG
  229. if ( gs_debug['f'] || gs_debug['F'] )
  230.        {    dputs("[f]calls alloc   up   down  step slowx  iter  find  band bstep bfill\n");
  231.         dprintf4("   %5ld %5ld %5ld %5ld",
  232.             n_fill, n_fill_alloc, n_y_up, n_y_down);
  233.         dprintf4(" %5ld %5ld %5ld %5ld",
  234.             n_x_step, n_slow_x, n_iter, n_find_y);
  235.         dprintf3(" %5ld %5ld %5ld\n",
  236.             n_band, n_band_step, n_band_fill);
  237.        }
  238. #endif
  239.     return code;
  240. }
  241.  
  242. /* Create a line list for a (flattened) path. */
  243. /* We pass in the list size, so that we can use this to iterate */
  244. /* over more than one path simultaneously (needed for clipping). */
  245. private int
  246. alloc_line_list(ll_ptr ll, uint sub_count)
  247. {    ll->active_area = 0;
  248.     ll->close_count = sub_count;
  249.     ll->close_area =
  250.       (sub_count <= max_local_close ?
  251.        ll->local_close :
  252.        (line_close_segment *)gs_malloc(sub_count, sizeof(line_close_segment),
  253.                        "closing lines"));
  254.     ll->next_line = ll->close_area;
  255.     if ( ll->close_area == 0 )
  256.         return_error(gs_error_VMerror);
  257.     ll->next_active = ll->local_active;
  258.     ll->limit = ll->next_active + max_local_active;
  259.     ll->y_list = 0;
  260.     ll->y_line = 0;
  261.     stat(n_fill);
  262.     return 0;
  263. }
  264.  
  265. /* Free the line list */
  266. private void
  267. free_line_list(ll_ptr ll)
  268. {    line_close_segment *lp;
  269.     active_line *alp;
  270.     /* Splice out any inserted closing lines */
  271.     for ( lp = ll->close_area; lp != ll->next_line; lp++ )
  272.        {    segment *prev = lp->prev, *next = lp->next;
  273.         prev->next = next;
  274.         if ( next ) next->prev = prev;
  275.         lp->sub->last = prev;
  276.        }
  277.     /* Free any individually allocated active_lines. */
  278.     while ( (alp = ll->active_area) != 0 )
  279.        {    active_line *next = alp->alloc_next;
  280.         gs_free((char *)alp, 1, sizeof(active_line),
  281.             "active line");
  282.         ll->active_area = next;
  283.        }
  284.     /* Free any separately allocated closing lines. */
  285.     if ( ll->close_area != ll->local_close && ll->close_area != 0 )
  286.        {    gs_free((char *)ll->close_area, ll->close_count,
  287.             sizeof(line_close_segment), "closing lines");
  288.        }
  289. }
  290.  
  291. /* Construct a Y-sorted list of lines for a (flattened) path. */
  292. /* We assume the path is non-empty.  Only include non-horizontal */
  293. /* lines where one endpoint is locally Y-minimal. */
  294. private int
  295. add_y_list(gx_path *ppath, short tag, ll_ptr ll)
  296. {    register segment *pseg = (segment *)ppath->first_subpath;
  297.     subpath *psub;
  298.     segment *plast;
  299.     int first_dir, prev_dir, dir;
  300.     segment *prev;
  301.     fixed xmin = ll->box.p.x, ymin = ll->box.p.y;
  302.     fixed xmax = ll->box.q.x, ymax = ll->box.q.y;
  303.     int code;
  304.     ll->tag = tag;
  305.     while ( pseg )
  306.        {    switch ( pseg->type )
  307.            {    /* No curves left */
  308.         case s_start:
  309.             psub = (subpath *)pseg;
  310.             plast = psub->last;
  311.             dir = 2;    /* hack to skip first line */
  312.             if ( plast->type != s_line_close )
  313.                {    /* Create a fake s_line_close */
  314.                 line_close_segment *lp = ll->next_line++;
  315.                 segment *next = plast->next;
  316.                 lp->next = next;
  317.                 lp->prev = plast;
  318.                 plast->next = (segment *)lp;
  319.                 if ( next ) next->prev = (segment *)lp;
  320.                 lp->type = s_line_close;
  321.                 lp->pt = psub->pt;
  322.                 lp->sub = psub;
  323.                 plast = (segment *)lp;
  324.                 psub->last = plast;
  325.                }
  326.             break;
  327.         default:        /* s_line, _close */
  328.            {    fixed iy = pseg->pt.y;
  329.             fixed py = prev->pt.y;
  330.             /* Lines falling entirely outside the ibox */
  331.             /* are treated as though they were horizontal, */
  332.             /* i.e., they are never put on the list. */
  333. #define compute_dir(xo, xe, yo, ye)\
  334.   (xo > xmax && xe > xmax ? 0 :\
  335.    ye > yo ? (ye <= ymin || yo >= ymax ? 0 : dir_up) :\
  336.    ye < yo ? (yo <= ymin || ye >= ymax ? 0 : dir_down) :\
  337.    0)
  338. #define add_dir_lines(prev2, prev, this, pdir, dir)\
  339.   if ( pdir )\
  340.    { if ( (code = add_y_line(prev2, prev, pdir, ll)) < 0 ) return code; }\
  341.   if ( dir )\
  342.    { if ( (code = add_y_line(prev, this, dir, ll)) < 0 ) return code; }
  343.             dir = compute_dir(prev->pt.x, pseg->pt.x, py, iy);
  344.             if ( dir > prev_dir )
  345.                {    add_dir_lines(prev->prev, prev, pseg, prev_dir, dir);
  346.                }
  347.             else if ( prev_dir == 2 )    /* first line */
  348.                 first_dir = dir;
  349.             if ( pseg == plast )
  350.                {    /* We skipped the first segment of the */
  351.                 /* subpath, so the last segment must */
  352.                 /* receive special consideration. */
  353.                 /* Note that we have `closed' all subpaths. */
  354.                 if ( first_dir > dir )
  355.                    {    add_dir_lines(prev, pseg, psub->next, dir, first_dir);
  356.                    }
  357.                }
  358.            }
  359. #undef compute_dir
  360. #undef add_dir_lines
  361.            }
  362.         prev = pseg;
  363.         prev_dir = dir;
  364.         pseg = pseg->next;
  365.        }
  366.     return 0;
  367. }
  368. /* Internal routine to test a line segment and add it to the */
  369. /* pending list if appropriate. */
  370. private int
  371. add_y_line(segment *prev_lp, segment *lp, int dir, ll_ptr ll)
  372. {    gs_fixed_point this, prev;
  373.     register active_line *alp = ll->next_active;
  374.     fixed y_start;
  375.     if ( alp == ll->limit )
  376.        {    /* Allocate separately */
  377.         alp = (active_line *)gs_malloc(1, sizeof(active_line), "active line");
  378.         if ( alp == 0 ) return_error(gs_error_VMerror);
  379.         alp->alloc_next = ll->active_area;
  380.         ll->active_area = alp;
  381.         stat(n_fill_alloc);
  382.        }
  383.     else
  384.         ll->next_active++;
  385.     this.x = lp->pt.x;
  386.     this.y = lp->pt.y;
  387.     prev.x = prev_lp->pt.x;
  388.     prev.y = prev_lp->pt.y;
  389.     alp->tag = ll->tag;
  390.     if ( (alp->direction = dir) > 0 )
  391.        {    /* Upward line */
  392.         y_start = prev.y;
  393.         set_al_points(alp, prev, this);
  394.         alp->pseg = lp;
  395.        }
  396.     else
  397.        {    /* Downward line */
  398.         y_start = this.y;
  399.         set_al_points(alp, this, prev);
  400.         alp->pseg = prev_lp;
  401.        }
  402.     /* Insert the new line in the Y ordering */
  403.        {    register active_line *yp = ll->y_line;
  404.         register active_line *nyp;
  405.         if ( yp == 0 )
  406.            {    alp->next = alp->prev = 0;
  407.             ll->y_list = alp;
  408.            }
  409.         else if ( y_start >= yp->start.y )
  410.            {    /* Insert the new line after y_line */
  411.             while ( stat(n_y_up), (nyp = yp->next) != NULL && y_start > nyp->start.y )
  412.                 yp = nyp;
  413.             alp->next = nyp;
  414.             alp->prev = yp;
  415.             yp->next = alp;
  416.             if ( nyp ) nyp->prev = alp;
  417.            }
  418.         else
  419.            {    /* Insert the new line before y_line */
  420.             while ( stat(n_y_down), (nyp = yp->prev) != NULL && y_start < nyp->start.y )
  421.                 yp = nyp;
  422.             alp->prev = nyp;
  423.             alp->next = yp;
  424.             yp->prev = alp;
  425.             if ( nyp ) nyp->next = alp;
  426.             else ll->y_list = alp;
  427.            }
  428.        }
  429.     ll->y_line = alp;
  430.     print_al("add ", alp);
  431.     return 0;
  432. }
  433.  
  434. /* Main filling loop.  Takes lines off of y_list and adds them to */
  435. /* x_list as needed. */
  436. private void
  437. fill_loop(gx_device_color *pdevc, int rule, ll_ptr ll,
  438.   gs_state *pgs, fixed adjust)
  439. {    fixed adj2 = adjust << 1;
  440.     active_line *yll = ll->y_list;
  441.     gs_fixed_point pmax;
  442.     fixed y;
  443.     if ( yll == 0 ) return;        /* empty list */
  444.     pmax = ll->box.q;
  445.     y = yll->start.y;        /* first Y value */
  446.     ll->x_list = 0;
  447.     ll->x_head.x_current = min_fixed;    /* stop backward scan */
  448.     while ( 1 )
  449.        {    fixed y1, y0;
  450.         active_line *endp, *alp, *stopx;
  451.         fixed x;
  452.         int draw;
  453.         stat(n_iter);
  454.         /* Check whether we've reached the maximum y. */
  455.         if ( y >= pmax.y ) break;
  456.         /* Move newly active lines from y to x list. */
  457.         while ( yll != 0 && yll->start.y == y )
  458.            {    active_line *ynext = yll->next;    /* insert smashes next/prev links */
  459.             insert_x_new(yll, ll);
  460.             yll = ynext;
  461.            }
  462.         if ( ll->x_list == 0 )
  463.            {    /* No active lines, skip to next start */
  464.             if ( yll == 0 ) break;    /* no lines left */
  465.             y = yll->start.y;
  466.             continue;
  467.            }
  468.         /* Find the next evaluation point. */
  469.         /* Start by finding the smallest y value */
  470.         /* at which any currently active line ends */
  471.         /* (or the next to-be-active line begins). */
  472.         y1 = (yll != 0 ? yll->start.y : max_fixed);
  473.         for ( alp = ll->x_list; alp != 0; alp = alp->next )
  474.           if ( alp->end.y < y1 ) y1 = alp->end.y;
  475. #ifdef DEBUG
  476. if ( gs_debug['F'] )
  477.    {        dprintf2("[f]before loop: y=%f y1=%f:\n",
  478.                  fixed2float(y), fixed2float(y1));
  479.         print_line_list(ll->x_list);
  480.    }
  481. #endif
  482.         /* Now look for line intersections before y1. */
  483.         x = min_fixed;
  484.         y0 = y - adjust;
  485. #define have_pixels() (fixed_rounded(y0) < fixed_rounded(y1 + adjust))
  486.         draw = (have_pixels() ? 1 : -1);
  487.         /*
  488.          * Loop invariants:
  489.          *    alp = endp->next;
  490.          *    for all lines lp from stopx up to alp,
  491.          *      lp->x_next = al_x_at_y(lp, y1).
  492.          */
  493.         for ( alp = stopx = ll->x_list; stat(n_find_y), alp != 0;
  494.              endp = alp, alp = alp->next
  495.             )
  496.            {    fixed nx = al_x_at_y(alp, y1);
  497.             fixed dx_old, dx_den;
  498.             /* Check for intersecting lines. */
  499.             if ( nx >= x )
  500.                 x = nx;
  501.             else if
  502.                ( draw >= 0 && /* don't bother if no pixels */
  503.                  (dx_old = alp->x_current - endp->x_current) >= 0 &&
  504.                  (dx_den = dx_old + endp->x_next - nx) > dx_old
  505.                )
  506.                {    /* Make a good guess at the intersection */
  507.                 /* Y value using only local information. */
  508.                 fixed dy = y1 - y, y_new;
  509. #ifdef DEBUG
  510. if ( gs_debug['f'] || gs_debug['F'] )
  511.                 dprintf3("[f]cross: dy=%g, dx_old=%g, dx_new=%g\n",
  512.                   fixed2float(dy), fixed2float(dx_old),
  513.                   fixed2float(dx_den - dx_old));
  514. #endif
  515.                 /* Do the computation in single precision */
  516.                 /* if the values are small enough. */
  517.                 y_new =
  518.                   ((dy | dx_old) < 1L << (sizeof(fixed)*4-1) ?
  519.                    dy * dx_old / dx_den :
  520.                    fixed_mult_quo(dy, dx_old, dx_den))
  521.                   + y;
  522.                 /* The crossing value doesn't have to be */
  523.                 /* very accurate, but it does have to be */
  524.                 /* greater than y and less than y1. */
  525. #ifdef DEBUG
  526. if ( gs_debug['f'] || gs_debug['F'] )
  527.                 dprintf3("[f]cross y=%g, y_new=%g, y1=%g\n",
  528.                   fixed2float(y), fixed2float(y_new),
  529.                   fixed2float(y1));
  530. #endif
  531.                 stopx = alp;
  532.                 if ( y_new <= y ) y_new = y + 1;
  533.                 if ( y_new < y1 )
  534.                    {    y1 = y_new;
  535.                     nx = al_x_at_y(alp, y1);
  536.                     draw = 0;
  537.                    }
  538.                 if ( nx > x ) x = nx;
  539.                }
  540.             alp->x_next = nx;
  541.            }
  542.         /* Recompute next_x for lines before the intersection. */
  543.         for ( alp = ll->x_list; alp != stopx; alp = alp->next )
  544.             alp->x_next = al_x_at_y(alp, y1);
  545. #ifdef DEBUG
  546. if ( gs_debug['F'] )
  547.    {        dprintf1("[f]after loop: y1=%f\n", fixed2float(y1));
  548.         print_line_list(ll->x_list);
  549.    }
  550. #endif
  551.         /* Fill a multi-trapezoid band for the active lines. */
  552.         /* Don't bother if no pixel centers lie within the band. */
  553.         if ( draw > 0 || draw == 0 && have_pixels() )
  554.            {    active_line *alp = ll->x_list;
  555.             fixed height = y1 - y + adj2;
  556.             fixed xlbot, xltop;    /* as of last "outside" line */
  557.             int inside[2];
  558.             inside[0] = 0;            /* 0 for path */
  559.             inside[1] = ll->no_clip;    /* 1 for clip path */
  560.             stat(n_band);
  561.             x = min_fixed;
  562.             /* rule = -1 for winding number rule, i.e. */
  563.             /* we are inside if the winding number is non-zero; */
  564.             /* rule = 1 for even-odd rule, i.e. */
  565.             /* we are inside if the winding number is odd. */
  566.             /* Clever, eh? */
  567. #define inside_path_p() ((inside[0] & rule) && (inside[1] & pgs->clip_rule))
  568.             while ( alp != 0 )
  569.                {    fixed xbot = alp->x_current;
  570.                 fixed xtop = alp->x_next;
  571.                 print_al("step", alp);
  572.                 stat(n_band_step);
  573.                 if ( inside_path_p() )
  574.                  { inside[alp->tag] += alp->direction;
  575.                    if ( !inside_path_p() )    /* about to go out */
  576.                     {    fixed wbot = xbot - xlbot + adj2;
  577.                     fixed wtop = xtop - xltop + adj2;
  578.                     stat(n_band_fill);
  579.                     /* If lines are temporarily out of */
  580.                     /* order, wtop might be negative. */
  581.                     /* Patch this up now. */
  582.                     if ( wtop < 0 )
  583.                        {    xltop += wtop >> 1;
  584.                         wtop = 0;
  585.                        }
  586.                     gz_fill_trapezoid_fixed(xlbot - adjust,
  587.                          wbot, y0,
  588.                          xltop - adjust, wtop,
  589.                          height, 0, pdevc, pgs);
  590.                     }
  591.                  }
  592.                 else            /* outside */
  593.                    {    inside[alp->tag] += alp->direction;
  594.                     if ( inside_path_p() )    /* about to go in */
  595.                         xlbot = xbot, xltop = xtop;
  596.                    }
  597.                 alp = alp->next;
  598.                }
  599.            }
  600.         update_x_list(ll->x_list, y1);
  601.         y = y1;
  602.        }
  603. }
  604.  
  605. /* Insert a newly active line in the X ordering. */
  606. private void
  607. insert_x_new(active_line *alp, ll_ptr ll)
  608. {    register active_line *next;
  609.     register active_line *prev = &ll->x_head;
  610.     register fixed x = alp->start.x;
  611.     alp->x_current = x;
  612.     while ( stat(n_x_step),
  613.         (next = prev->next) != 0 && x_precedes(next, alp, x)
  614.            )
  615.         prev = next;
  616.     alp->next = next;
  617.     alp->prev = prev;
  618.     if ( next != 0 ) next->prev = alp;
  619.     prev->next = alp;
  620. }
  621.  
  622. /* Clean up after a pass through the main loop. */
  623. /* If any lines are out of order, re-sort them now. */
  624. /* Also drop any ended lines. */
  625. private void
  626. update_x_list(active_line *x_first, fixed y1)
  627. {    fixed x;
  628.     register active_line *alp;
  629.     active_line *nlp;
  630.     for ( x = min_fixed, alp = x_first; alp != 0; alp = nlp )
  631.        {    fixed nx = alp->x_current = alp->x_next;
  632.         nlp = alp->next;
  633. #ifdef DEBUG
  634. if ( gs_debug['f'] || gs_debug['F'] )
  635.         dprintf4("[f]check %lx,x=%g %lx,x=%g\n",
  636.           (ulong)alp->prev, fixed2float(x),
  637.           (ulong)alp, fixed2float(nx));
  638. #endif
  639.         if ( alp->end.y == y1 )
  640.            {    /* Handle a line segment that just ended. */
  641.             segment *pseg = alp->pseg;
  642.             segment *next;
  643.             gs_fixed_point npt;
  644.             /*
  645.              * The computation of next relies on the fact that
  646.              * all subpaths have been closed.  When we cycle
  647.              * around to the other end of a subpath, we must be
  648.              * sure not to process the start/end point twice.
  649.              */
  650.             next =
  651.               (alp->direction == dir_up ?
  652.                (/* Upward line, go forward along path. */
  653.                 pseg->type == s_line_close ? /* end of subpath */
  654.                  ((line_close_segment *)pseg)->sub->next :
  655.                  pseg->next) :
  656.                (/* Downward line, go backward along path. */
  657.                 pseg->type == s_start ? /* start of subpath */
  658.                  ((subpath *)pseg)->last->prev :
  659.                  pseg->prev)
  660.               );
  661.             npt.y = next->pt.y;
  662. #ifdef DEBUG
  663. if ( gs_debug['F'] )
  664.             dprintf5("[f]ended %lx: pseg=%lx y=%f next=%lx npt.y=%f\n",
  665.                  (ulong)alp, (ulong)pseg, fixed2float(pseg->pt.y),
  666.                  (ulong)next, fixed2float(npt.y));
  667. #endif
  668.             if ( npt.y <= pseg->pt.y )
  669.                {    /* End of a line sequence */
  670.                 alp->prev->next = nlp;
  671.                 if ( nlp ) nlp->prev = alp->prev;
  672. #ifdef DEBUG
  673. if ( gs_debug['F'] )
  674.                 dprintf1("[f]drop %lx\n", (ulong)alp);
  675. #endif
  676.                 continue;
  677.                }
  678.             else
  679.                {    alp->pseg = next;
  680.                 npt.x = next->pt.x;
  681.                 set_al_points(alp, alp->end, npt);
  682.                 print_al("repl", alp);
  683.                }
  684.            }
  685.         if ( nx <= x )
  686.            {    /* Move alp backward in the list. */
  687.             active_line *prev = alp->prev;
  688.             active_line *next = nlp;
  689.             prev->next = next;
  690.             if ( next ) next->prev = prev;
  691.             while ( !x_precedes(prev, alp, nx) )
  692.                {
  693. #ifdef DEBUG
  694. if ( gs_debug['f'] || gs_debug['F'] )
  695.                 dprintf2("[f]swap %lx,%lx\n",
  696.                          (ulong)alp, (ulong)prev);
  697. #endif
  698.                 next = prev, prev = prev->prev;
  699.                }
  700.             alp->next = next;
  701.             alp->prev = prev;
  702.             /* next might be null, if alp was in */
  703.             /* the correct spot already. */
  704.             if ( next ) next->prev = alp;
  705.             prev->next = alp;
  706.            }
  707.         else
  708.             x = nx;
  709.        }
  710. #ifdef DEBUG
  711. if ( gs_debug['f'] || gs_debug['F'] )
  712.     for ( alp = x_first; alp != 0; alp = alp->next )
  713.      if ( alp->next != 0 && alp->next->x_current < alp->x_current )
  714.        {    lprintf("[f]Lines out of order!\n");
  715.         print_active_line("   1:", alp);
  716.         print_active_line("   2:", alp->next);
  717.         gs_exit(1);
  718.        }
  719. #endif
  720. }
  721.